﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FootballProject.DAL.DatatAccessLib;

namespace FootballProject.BL.BusinessManagers
{
    /// <summary>
    /// Author: Ivan Shikht
    /// </summary>
    public class TeamManager
    {
        #region Fields
        private readonly EntitiesContext _context;
        #endregion

        #region Constructors
        public TeamManager()
        {
            _context = new EntitiesContext();
        }
        #endregion

        #region Methods
        public List<Team> FindAll()
        {
            IQueryable<Team> rTeam = from team in _context.TeamSet
                                     select team;
            return rTeam.ToList();
        }

        public List<Team> FindTeamsByName(string teamName)
        {
            IQueryable<Team> rTeam = from team in _context.TeamSet
                                     where team.TeamName.StartsWith(teamName)
                                     select team;
            return rTeam.ToList();
        }

        public Team FindTeamById(Guid teamId)
        {
            IQueryable<Team> rTeam = from team in _context.TeamSet
                                     where team.TeamID == teamId
                                     select team;
            return rTeam.First();
        }

        public void UpdateTeam(Team team)
        {
            _context.Attach(team);
            team.SetAllModified(_context);
            _context.SaveChanges();
        }

        public void AddTeam(Team team)
        {
            _context.AddToTeamSet(team);
            _context.SaveChanges();
        }


        public void DeleteTeam(Team team)
        {
            _context.Attach(team);
            _context.DeleteObject(team);
            _context.SaveChanges();
        }
        #endregion
    }
}
